package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.core.exception.Asserts;
import com.wsoft.system.dto.TsMutiLangDto;
import com.wsoft.system.entity.TsMutiLangEntity;
import com.wsoft.system.manager.TsMutiLangManager;
import com.wsoft.system.mapper.TsMutiLangMapper;
import com.wsoft.system.query.lang.TsMutiLangReq;
import com.wsoft.system.service.ITsMutiLangService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.utils.ResourceUtil;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年08月25日 18:35:42
 */
@Service
public class TsMutiLangServiceImpl extends ServiceImpl<TsMutiLangMapper, TsMutiLangEntity> implements ITsMutiLangService {

    @Resource
    private TsMutiLangManager tsMutiLangManager;
    @Override
    @PostConstruct
    public void initAllMutiLang() {
        //先清空
        ResourceUtil.mutiLangMap.clear();
        List<TsMutiLangEntity> mutiLang = this.list();
        //所有多语言配置放入内存中
        for (TsMutiLangEntity mutiLangEntity : mutiLang) {
            ResourceUtil.mutiLangMap.put(mutiLangEntity.getType() + "_" + mutiLangEntity.getCode(), mutiLangEntity.getName());
        }
    }

    @Override
    public boolean insert(TsMutiLangDto dto) {
        TsMutiLangEntity entity = Convert.convert(TsMutiLangEntity.class, dto);
        QueryWrapper<TsMutiLangEntity> query = new QueryWrapper<>();
        query.lambda().eq(TsMutiLangEntity::getCode, entity.getCode())
                .eq(TsMutiLangEntity::getType,entity.getType());
        if (count(query)>0){
            Asserts.fail("[sys.code.exist]");
        }
        boolean b = save(entity);
        if (b){
            //放入内存中
            ResourceUtil.mutiLangMap.put(entity.getType() + "_" + entity.getCode(), entity.getName());
        }
        return b;
    }

    @Override
    public boolean update(TsMutiLangDto dto) {
        TsMutiLangEntity entity = Convert.convert(TsMutiLangEntity.class, dto);
        QueryWrapper<TsMutiLangEntity> query = new QueryWrapper<>();
        query.lambda().eq(TsMutiLangEntity::getCode, entity.getCode())
                .eq(TsMutiLangEntity::getType, entity.getType())
                .ne(TsMutiLangEntity::getId, entity.getId());
        if (count(query)>0){
            Asserts.fail("[sys.code.exist]");
        }
        boolean b = updateById(entity);
        if (b){
            initAllMutiLang();
        }
        return b;
    }

    @Override
    public boolean delete(Long id) {
        boolean b = removeById(id);
        if (b){
            initAllMutiLang();
        }
        return b;
    }

    @Override
    public PageInfo<TsMutiLangEntity> pageMutiLang(TsMutiLangReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        QueryWrapper<TsMutiLangEntity> query = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(req.getType())){
            query.lambda().eq(TsMutiLangEntity::getType, req.getType());
        }
        if (StrUtil.isNotEmpty(req.getCode())){
            query.lambda().like(TsMutiLangEntity::getCode, req.getCode());
        }
        if (StrUtil.isNotEmpty(req.getName())){
            query.lambda().like(TsMutiLangEntity::getName, req.getName());
        }
        return PageInfo.of(list(query));
    }
}
